package com.lz.ovuola.web.api;

import java.util.Date;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lz.ovuola.service.PhysiologicalBasisService;
import com.lz.ovuola.service.PhysiologicalDataService;
import com.lz.ovuola.service.PhysiologicalReferencevDataService;
import com.lz.ovuola.util.annotation.ApiRestController;
import com.lz.ovuola.util.exception.BusinessException;
import com.lz.ovuola.util.response.ResponseDataPackageUtils;
import com.lz.ovuola.util.threadlocal.UserContextHolder;

/**
 * 生理周期控制层
 * 
 * @author fz
 *
 */
@RestController
@RequestMapping("/physiology/")
public class PhysiologicalController extends AbstractRestController {

	@Resource
	private PhysiologicalBasisService physiologicalBasisService;

	@Resource
	private PhysiologicalDataService physiologicalDataService;

	@Resource
	private PhysiologicalReferencevDataService physiologicalReferencevDataService;
	private Logger logger = LoggerFactory.getLogger("PhysiologicalController");

	/**
	 * 上传生理周期信息
	 * 
	 * @param physiologicalCommand
	 * @return
	 */
	@ApiRestController(isRight = true)
	@RequestMapping(value = "{userId}/info", method = RequestMethod.POST)
	public String save(@RequestBody PhysiologicalCommand physiologicalCommand,
			@PathVariable int userId) {
		logger.debug("用户id{},startTime{}", UserContextHolder.get().getUserId(),
				userId, physiologicalCommand.getStartTime());
		physiologicalBasisService.save(physiologicalCommand
				.getPhysiologicalLength(), physiologicalCommand
				.getMenstruationLength(), physiologicalCommand
				.getCycleFloatingDays(), physiologicalCommand.getStartTime(),
				UserContextHolder.get().getUserId());
		return ResponseDataPackageUtils.success();
	}

	/**
	 * 获取生理周期信息
	 * 
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@ApiRestController(isRight = true)
	@RequestMapping(value = "{userId}/info", method = RequestMethod.GET)
	public String get(@PathVariable int userId) throws Exception {
		return physiologicalBasisService.get(userId);
	}

	/**
	 * 获取例假期记录
	 * 
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@ApiRestController(isRight = true)
	@RequestMapping(value = "{userId}/menstruation/info", method = RequestMethod.GET)
	public String getMenst(@PathVariable int userId) throws Exception {
		return physiologicalBasisService.getMenst(userId);
	}

	/**
	 * 新增例假期信息
	 * 
	 * @param physiologicalCommand
	 * @param userId
	 * @return
	 */
	@ApiRestController(isRight = true)
	@RequestMapping(value = "{userId}/menstruation/info", method = RequestMethod.POST)
	public String add(@RequestBody PhysiologicalCommand physiologicalCommand,
			@PathVariable int userId) {
		physiologicalBasisService.add(physiologicalCommand.getStartTime(),
				physiologicalCommand.getEndTime(),
				physiologicalCommand.getMenstruationDate(), userId);
		return ResponseDataPackageUtils.success();
	}

	/**
	 * 修改例假期信息
	 * 
	 * @param physiologicalCommand
	 * @param userId
	 * @return
	 */
	@ApiRestController(isRight = true)
	@RequestMapping(value = "{userId}/menstruation/info", method = RequestMethod.PUT)
	public String update(
			@RequestBody PhysiologicalCommand physiologicalCommand,
			@PathVariable int userId) {
		physiologicalBasisService.update(physiologicalCommand.getStartTime(),
				physiologicalCommand.getEndTime(),
				physiologicalCommand.getMenstruationDate(), userId);
		return ResponseDataPackageUtils.success();
	}

	/**
	 * 上传硬件检测数据
	 * 
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@ApiRestController(isRight = true)
	@RequestMapping(value = "{userId}/resistances", method = RequestMethod.POST)
	public String saveData(@PathVariable int userId,
			@RequestBody PhysiologicalDataCommand physiologicalDataCommand)
			throws Exception {
		return physiologicalDataService.saveData(
				physiologicalDataCommand.getDetectionTime(),
				physiologicalDataCommand.getCh1rh(),
				physiologicalDataCommand.getCh1rl(),
				physiologicalDataCommand.getCh2rh(),
				physiologicalDataCommand.getCh2rl(),
				physiologicalDataCommand.getCh3r(),
				physiologicalDataCommand.getCh1vl(),
				physiologicalDataCommand.getCh2vl(),
				physiologicalDataCommand.getSick(),
				physiologicalDataCommand.getLeedingGums(), userId);
	}

	/**
	 * 获取历史检测记录
	 * 
	 * @param userId
	 * @param physiologicalDataCommand
	 * @return
	 * @throws Exception
	 */
	@ApiRestController(isRight = true)
	@RequestMapping(value = "{userId}/resistances", method = RequestMethod.GET)
	public String get(@PathVariable int userId,
			PhysiologicalDataCommand physiologicalDataCommand) throws Exception {
		Date begin = new Date();
		logger.info("任务开始计时...", begin);
		String rev = physiologicalDataService.get(
				physiologicalDataCommand.getStartTime(),
				physiologicalDataCommand.getEndTime());
		Date end = new Date();
		logger.info("任务结束，当前时间{} 时间差 {}", end, end.getTime() - begin.getTime());
		return rev;

	}

	/**
	 * 获取最新基准值
	 * 
	 * @param userId
	 * @return
	 * @throws BusinessException
	 */
	@ApiRestController(isRight = true)
	@RequestMapping(value = "{userId}/reference/info", method = RequestMethod.GET)
	public String getRe(@PathVariable int userId) throws BusinessException {

		return physiologicalReferencevDataService.get(userId);
	}
}
